package org.com.core.module;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import org.htmlcleaner.HtmlCleaner;
import org.htmlcleaner.TagNode;
import org.htmlcleaner.XPatherException;

public abstract class AbstractParser {

	public static final String REG_STRING = "//div[@class='watch-sidebar-body']/ul[@id='watch-related']/li/a";
	protected String defaultPath = "http://www.youtube.com/watch?v=CMTERek_di4";
	
	
	public ArrayList<ListVideo>  parserHtml() throws XPatherException {
		
		ArrayList<ListVideo> list = new ArrayList<ListVideo>();
		HtmlCleaner cleaner = new HtmlCleaner();
		TagNode html;
		try {
				html = cleaner.clean(new File("default.html"));
				Object []title = html.evaluateXPath(REG_STRING + "/span[position()=2]/text()");
				Object []path = html.evaluateXPath(REG_STRING);
				Object []rating = html.evaluateXPath(REG_STRING + "/span[position()=4]/text()");
				defaultPath.substring(0, defaultPath.lastIndexOf("/"));
				for(int i = 0; i < title.length; i++) {
					list.add(new ListVideo(title[i].toString().trim(),defaultPath +  ((TagNode)path[i]).getAttributeByName("href").trim(), rating[i].toString().lastIndexOf(";") > 0 ?  rating[i].toString().substring(rating[i].toString().lastIndexOf(";") + 1) : rating[i].toString().trim()));
				}
		} catch (IOException e) {
			
		}
		return list;
	}
	
	public abstract void LoadHtml(String filePath) throws IOException;
}